package algotithm.leetcode.dp.hot.test121;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/8-22:50
 * @Title 121. 买卖股票的最佳时机
 * @URL https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] prices = {7,1,5,3,6,4};
        int i = solution.maxProfit(prices);
        System.out.println(i);
    }

    public int maxProfit(int[] prices) {
        // 这里有个细节，最大的收益的最小值为0，不能设置为Integer.MAX_VALUE 反例[7,6,5,4,3,2,1]
        int maxProfit = 0;
        int minPrice = Integer.MAX_VALUE;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < minPrice) {
                minPrice = prices[i];
            } else if (prices[i] - minPrice > maxProfit) {
                maxProfit = prices[i] - minPrice;
            }
        }
        return maxProfit ;
    }

}
